package queue;

/**
 * @author lynn
 * @date 18-12-14 上午6:24
 */
public class LoopQueue<E> implements Queue<E> {

    private E[] data;
    private int front, tail;
    private int size;

    public LoopQueue(int capacity) {
        data = (E[]) new Object[capacity + 1];
        front = 0;
        tail = 0;
        size = 0;
    }

    public LoopQueue() {
        this(10);
    }

    public int getCapacity() {
        return this.data.length - 1;
    }

    /**
     * get queue size
     *
     * @return
     */
    @Override
    public int getSize() {
        return size;
    }

    /**
     * get queue is empty or not
     *
     * @return
     */
    @Override
    public boolean isEmpty() {
        return front == tail;
    }

    /**
     * put an element to the queue
     *
     * @param e
     */
    @Override
    public void enqueue(E e) {

        if ((tail + 1) % data.length == front) {
            resize(getCapacity() * 2);
        }

        data[tail] = e;
        tail = (tail + 1) % data.length;
        size++;

    }

    private void resize(int newCapacity) {

        E[] newData = (E[]) new Object[newCapacity + 1];

        for (int i = 0; i < size; i++) {
            newData[i] = data[(i + front) % data.length];
        }

        data = newData;
        front = 0;
        tail = size;


    }


    /**
     * delete an element from the queue
     *
     * @return
     */
    @Override
    public E dequeue() {

        if (this.isEmpty()) {
            throw new IllegalArgumentException("Cannot dequeue from an empty queue.");
        }

        E ret = data[front];
        data[front] = null;
        front = (front + 1) % data.length;
        size--;

        if (size == this.getCapacity() / 4 && this.getCapacity() / 2 != 0) {
            resize(getCapacity() / 2);
        }

        return ret;
    }

    /**
     * get an element from the queue
     *
     * @return
     */
    @Override
    public E getFront() {

        if (this.isEmpty()) {
            throw new IllegalArgumentException("Queue is empty");
        }

        return data[front];
    }

    /**
     * Returns a string representation of the object. In general, the
     * {@code toString} method returns a string that
     * "textually represents" this object. The result should
     * be a concise but informative representation that is easy for a
     * person to read.
     * It is recommended that all subclasses override this method.
     * <p>
     * The {@code toString} method for class {@code Object}
     * returns a string consisting of the name of the class of which the
     * object is an instance, the at-sign character `{@code @}', and
     * the unsigned hexadecimal representation of the hash code of the
     * object. In other words, this method returns a string equal to the
     * value of:
     * <blockquote>
     * <pre>
     * getClass().getName() + '@' + Integer.toHexString(hashCode())
     * </pre></blockquote>
     *
     * @return a string representation of the object.
     */
    @Override
    public String toString() {

        StringBuilder res = new StringBuilder();
        res.append(String.format("Queue: size = %d , capacity = %d\n", size, getCapacity()));
        res.append("front [");
        for (int i = front; i != tail; i = (i + 1) % data.length ) {
            res.append(data[i]);
            if ( (i + 1) % data.length != tail ) {
                res.append(",");
            }
        }
        res.append("] tail");
        return res.toString();


    }
}
